package org.whatever.aha.zjut.platform.task;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.whatever.aha.zjut.base.constant.StatusConstant;
import org.whatever.aha.zjut.platform.entity.competition.CompetitionStageStatus;
import org.whatever.aha.zjut.platform.entity.project.Project;
import org.whatever.aha.zjut.platform.entity.school.ProjectScore;
import org.whatever.aha.zjut.platform.service.competition.CompetitionService;
import org.whatever.aha.zjut.platform.service.competition.CompetitionStageService;
import org.whatever.aha.zjut.platform.service.competition.CompetitionStageStatusService;
import org.whatever.aha.zjut.platform.service.project.ProjectService;
import org.whatever.aha.zjut.platform.service.school.ProjectScoreService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 判断公示后计算评委均分落库projectScore
 *
 * @author Vc
 * @date 2022/03/15 13:06
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class PublicStageStatusTask {
    final ProjectService projectService;
    final CompetitionStageStatusService competitionStageStatusService;
    final CompetitionStageService competitionStageService;
    final ProjectScoreService projectScoreService;

    //半小时更新一次全量数据
    @Scheduled(cron = "0 */30 * * * ?")
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus() {
        log.info("开始执行公示阶段评委均分落库");
        //获得所有未完结的阶段Id
        List<Object> ongoingStages = competitionStageService.getOngoingStageId();
        Date now = new Date();
        ongoingStages.stream().forEach(stageId -> {
            CompetitionStageStatus singleCompStageStatus = competitionStageStatusService.getSingleCompStageStatus((Integer) stageId, StatusConstant.COMPETITION_STAGE_STATUS_ORDER_PROMULGATE.getCode());
            if(now.before(singleCompStageStatus.getStatusStartTime()) && now.after(singleCompStageStatus.getStatusEndTime())){
                Map<Integer, Double> projectAvgScores = projectScoreService.getProjectScoreByStageId((Integer) stageId).stream().collect(
                        Collectors.groupingBy(
                                ProjectScore::getProjectId,
                                Collectors.averagingDouble(ProjectScore::getdoubleScore)
                        )
                );
                projectAvgScores.keySet().stream().forEach(projectId -> {
                    //如果均分为空就填上均分
                    if(projectService.getAvgScore(projectId) == null){
                        projectService.updateProject(Project.builder().projectId(projectId).projectScore(BigDecimal.valueOf(projectAvgScores.get(projectId))).build());
                        log.info(String.format("将projectId : %d 的均分落库为 %f", projectId, projectAvgScores.get(projectId)));
                    }
                });
            }
        });
        log.info("评委均分落库执行完成");
    }

}
